package com.xxwy.ddu.security.code.vaildatecode.sms;

import com.xxwy.ddu.security.code.properties.SecurityProperties;
import com.xxwy.ddu.security.code.vaildatecode.ValidateCode;
import com.xxwy.ddu.security.code.vaildatecode.ValidateCodeGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @author xxwy
 * on 2018/8/21 0021
 */
@Slf4j
@Component("smsValidateCodeGenerator")
public class SmsValidateCodeGenerator implements ValidateCodeGenerator {

    private SecurityProperties securityProperties;

    public SmsValidateCodeGenerator(SecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }

    @Override
    public ValidateCode generate(ServletWebRequest request) {
        long code1 = securityProperties.getCode().getSms().getCode();
        String code = Long.toString(RandomUtils.nextLong(code1,code1*10));
        log.info("生成的数字为 "+code);
        return new ValidateCode(code,securityProperties.getCode().getSms().getExpireIn());
    }



}
